package com.study.locks.redis;

/**
 * @author: chenhs
 * @date: Created in 6:48 2019/12/16
 **/
public class SellTicket_Redis implements Runnable {

    //100张票
    private int tickets = 100;

    RedisDistributeLock lock = new RedisDistributeLock();

    @Override
    public void run() {
        while (tickets > 0) {

            //开启锁
            lock.lock();
            //lock.tryLock();
            try {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //窗口号
                    System.out.println(Thread.currentThread().getName() + "正在出票第 " + (tickets--) + " 张票");
                }
            } finally {
                lock.unlock();
            }
        }
    }

}
